package jehc.djshi.log.web;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jehc.djshi.common.annotation.AuthUneedLogin;
import jehc.djshi.common.annotation.NeedLoginUnAuth;
import jehc.djshi.common.base.BaseAction;
import jehc.djshi.common.base.BasePage;
import jehc.djshi.common.base.BaseResult;
import jehc.djshi.common.base.BaseSearch;
import jehc.djshi.common.util.StringUtil;
import jehc.djshi.log.model.XtLoginLogs;
import jehc.djshi.log.service.XtLoginLogsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Desc 登录日志
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/xtLoginLogs")
@Api(value = "登录日志API",description = "登录日志API")
public class XtLoginLogsController extends BaseAction {
	@Autowired
	private XtLoginLogsService xtLoginLogsService;
	
	/**
	* 分页查询
	* @param baseSearch
	* @param flag
	*/
	@NeedLoginUnAuth
	@PostMapping(value="/list")
	@ApiOperation(value="分页查询", notes="分页查询")
	public BasePage getXtLoginLogsListByCondition(@RequestBody(required=true)BaseSearch baseSearch, String flag){
		Map<String, Object> condition = baseSearch.convert();
		if(null != flag){
			condition.put("xt_userinfo_id", getXtUid());
		}
		commonHPager(baseSearch);
		List<XtLoginLogs>XtLoginLogsList = xtLoginLogsService.getXtLoginLogsListByCondition(condition);
		PageInfo<XtLoginLogs> page = new PageInfo<XtLoginLogs>(XtLoginLogsList);
		return outPageBootStr(page,baseSearch);
	}
	/**
	* 查询单条记录
	* @param xt_login_log_id
	*/
	@NeedLoginUnAuth
	@GetMapping(value="/get/{xt_login_log_id}")
	@ApiOperation(value="查询单条记录", notes="查询单条记录")
	public BaseResult getXtLoginLogsById(@PathVariable("xt_login_log_id")String xt_login_log_id){
		XtLoginLogs xt_Login_Logs = xtLoginLogsService.getXtLoginLogsById(xt_login_log_id);
		return outDataStr(xt_Login_Logs);
	}
	
	/**
	* 删除
	* @param xt_login_log_id
	*/
	@DeleteMapping(value="/delete")
	@ApiOperation(value="删除登录日志", notes="删除登录日志")
	public BaseResult delXtLoginLogs(String xt_login_log_id){
		int i = 0;
		if(!StringUtil.isEmpty(xt_login_log_id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("xt_login_log_id",xt_login_log_id.split(","));
			i=xtLoginLogsService.delXtLoginLogs(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
	/**
	 * 统计出每个人登录的次数
	 * @param request
	 * @return
	 */
	@GetMapping(value="/group/list")
	@ApiOperation(value="统计出每个人登录的次数", notes="统计出每个人登录的次数")
	public BaseResult getGroupXtLoginLogsList(HttpServletRequest request){
		Map<String, Object> condition = new HashMap<String, Object>();
		List<XtLoginLogs> list = xtLoginLogsService.getGroupXtLoginLogsList(condition);
		return outItemsStr(list);
	}

	/**
	 * 个人登录的次数
	 * @param request
	 * @return
	 */
	@GetMapping(value="/times")
	@NeedLoginUnAuth
	@ApiOperation(value="个人登录的次数", notes="个人登录的次数")
	public BaseResult times(HttpServletRequest request){
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("xt_userinfo_id",getXtUid());
		int count = xtLoginLogsService.getXtLoginLogsCount(condition);
		return outItemsStr(count);
	}

	/**
	 * 添加
	 * @param xtLoginLogs
	 */
	@ApiOperation(value="新增登录日志", notes="新增登录日志")
	@PostMapping(value="/add")
	@AuthUneedLogin
	public BaseResult addOauthAccount(@RequestBody XtLoginLogs xtLoginLogs){
		int i = 0;
		if(null != xtLoginLogs){
			xtLoginLogs.setXt_login_log_id(toUUID());
			xtLoginLogs.setXt_login_logtime(getSimpleDateFormat());
			i=xtLoginLogsService.addXtLoginLogs(xtLoginLogs);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
}
